<?php

namespace App\Http\Controllers\Admin;

use App\Models\Classify;
use App\Models\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ClassifyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function lists(Request $request)
    {
        $pa_id=0;
        if($request->has('classify_id')){
            $pa_id=$request->get('classify_id');
        }
        $classify = Classify::where('pa_id',$pa_id)->orderBy('sort')->paginate(20);
        foreach ($classify as $value){
            $value->my_img;
        }
        if($request->isMethod('post')){
            return $classify;
        }
        return view('admin.classify_list',[
            'classify'=>$classify,'pa_id'=>$pa_id
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {

        $data = [
            'pa_id'=>$request->get('pa_id'),
            'name'=>$request->get('name'),
            'sort'=>$request->get('sort'),
            'level'=>$request->get('pa_id')==0?1:2,
        ];
        if ($request->hasFile('img')) {
            $img = $this->upload_img($request->img,'upload/classify',2);
            if($img['status']==1){
                $data['img'] = $img['img']['pic_id'];
            }else{
                return ['status_code'=>0,'message'=>$img['message']];
            }
        }
        return Classify::create($data)?
            redirect()->back()->with(['status'=>1,'message'=>"创建成功"]):
            redirect()->back()->with(['status'=>0,'message'=>"fail"]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $classify = Classify::find($request->get('classify_id'));
        if ($request->hasFile('img')) {
            $img = $this->upload_img($request->img,'upload/classify',2);
            if($img['status']==1){
                $classify->img = $img['img']['pic_id'];
            }else{
                return ['status_code'=>0,'message'=>$img['message']];
            }
        }
        $classify->name=$request->get('name');
        $classify->sort=$request->get('sort');
        return $classify->save()?
            redirect()->back()->with(['status'=>1,'message'=>"修改成功"]):
            redirect()->back()->with(['status'=>0,'message'=>"fail"]);
    }

    public function destory(Request $request)
    {
        $classify = Classify::find($request->get('classify_id'));
        if($classify->pa_id==0){
            $classify2 = Classify::where('pa_id',$classify->classify_id)->get();
            foreach ($classify2 as $value){
                Goods::where('class_id',$value->classify_id)->delete();
                $value->delete();
            }
        }
        $classify->delete();
        return redirect()->back()->with(['status'=>1,'message'=>"子分类及商品都已删除"]);
    }
}
